﻿using Elifart.Library;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Elifart.Editor.GeneralControls
{
    public partial class ChooseAthletesDialog : Form
    {
        #region Attributes and Properties

        private AthleteCollection _clonedToChooseAthletes;
        private AthleteCollection _clonedChosenAthletes;

        private AthleteCollection _toChooseAthletes;
        public AthleteCollection ToChooseAthletes
        {
            get { return this._toChooseAthletes; }
            set { this._toChooseAthletes = value; }
        }

        private AthleteCollection _chosenAthletes;
        public AthleteCollection ChosenAthletes
        {
            get { return this._chosenAthletes; }
            set { this._chosenAthletes = value; }
        }

        #endregion

        #region Private Members

        private bool _changed = false;

        #endregion

        #region Constructors

        public ChooseAthletesDialog(AthleteCollection toChooseAthletes, AthleteCollection chosenAthletes)
        {
            InitializeComponent();

            this._toChooseAthletes = toChooseAthletes;
            this._chosenAthletes = chosenAthletes;

            this._clonedToChooseAthletes = toChooseAthletes.Clone() as AthleteCollection;
            this._clonedChosenAthletes = chosenAthletes.Clone() as AthleteCollection;

            this.InitializeToChooseAthletes();
            this.InitializeChosenAthletes();
            this.InitializeButtons();
        }

        #endregion

        #region Private Methods

        private void InitializeToChooseAthletes()
        {
            this.lbToChooseAthletes.DataSource = this._clonedToChooseAthletes;
        }

        private void InitializeChosenAthletes()
        {
            this.lbChosenAthletes.DataSource = this._clonedChosenAthletes;
        }

        private void InitializeButtons()
        {
            this.btnChooseAthlete.Enabled = this.lbToChooseAthletes.SelectedItems.Count > 0;
            this.btnRemoveAthlete.Enabled = this.lbChosenAthletes.SelectedItems.Count > 0;
        }

        private void Save()
        {
            this._chosenAthletes.Clear();
            foreach (Athlete athlete in this._clonedChosenAthletes)
                this._chosenAthletes.Add(athlete);

            this._toChooseAthletes.Clear();
            foreach (Athlete athlete in this._clonedToChooseAthletes)
                this._toChooseAthletes.Add(athlete);
        }

        #endregion

        #region Signed Events Methods

        private void btnChooseAthlete_Click(object sender, EventArgs e)
        {
            if (this.lbToChooseAthletes.SelectedItems.Count == 0)
                return;

            List<Athlete> selectedAthletes = new List<Athlete>();

            foreach (Athlete athlete in this.lbToChooseAthletes.SelectedItems)
                selectedAthletes.Add(athlete);

            foreach (Athlete athlete in selectedAthletes)
            {
                this._clonedToChooseAthletes.Remove(athlete);
                this._clonedChosenAthletes.Add(athlete);
            }

            this.lbToChooseAthletes.SelectedItems.Clear();

            this._changed = true;
        }

        private void btnRemoveAthlete_Click(object sender, EventArgs e)
        {
            if (this.lbChosenAthletes.SelectedItems.Count == 0)
                return;

            List<Athlete> selectedAthletes = new List<Athlete>();

            foreach (Athlete athlete in this.lbChosenAthletes.SelectedItems)
                selectedAthletes.Add(athlete);

            foreach (Athlete athlete in selectedAthletes)
            {
                this._clonedChosenAthletes.Remove(athlete);
                this._clonedToChooseAthletes.Add(athlete);
            }

            this.lbChosenAthletes.SelectedItems.Clear();

            this._changed = true;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Save();

            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this._changed = false;
        }

        private void lbToChooseAthletes_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnChooseAthlete.Enabled = this.lbToChooseAthletes.SelectedItems.Count > 0;
        }

        private void lbChosenAthletes_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.btnRemoveAthlete.Enabled = this.lbChosenAthletes.SelectedItems.Count > 0;
        }

        private void ChooseAthletesDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult != DialogResult.OK && this._changed)
            {
                DialogResult result = MessageBox.Show("Deseja salvar as alterações?", "Salvar", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                    this.Save();
                else if (result == DialogResult.Cancel)
                    e.Cancel = true;
            }
        }

        #endregion
    }
}
